package com.ulic.customerManApplication.base;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.exception.SystemException;
import com.ulic.customerManApplication.pager.PagerModel;

/**
 * @project HxService
 * @Version 1.0
 * @author 吴积嵩
 * @Date 2010-11-02
 * @description DAO基础类
 */
public class BaseDao {
	protected HibernateTemplate hibernateTemplate;

	public PagerModel searchPaginated(String hql) {
		return searchPaginated(hql, null);
	}

	public PagerModel searchPaginated(String hql, Object param) {
		return searchPaginated(hql, new Object[] { param });
	}

	public PagerModel searchPaginated(String hql, Object[] params, int curPage) {
		return searchPaginated(hql, params, curPage, CONSTANT.PageSize);
	}

	public PagerModel searchPaginated(String hql, int curPage, int pageSize) {
		return searchPaginated(hql, null, curPage, pageSize);
	}

	public PagerModel searchPaginated(String hql, Object param, int curPage,
			int pageSize) {
		return searchPaginated(hql, new Object[] { param }, curPage, pageSize);
	}

	@SuppressWarnings("unchecked")
	public PagerModel searchPaginated(String hql, Object[] params, int curPage,
			int pageSize) {
		// 获取记录总数
		String countHql = getCountQuery(hql);
		Query query = hibernateTemplate.getSessionFactory().getCurrentSession()
				.createQuery(countHql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		int total = ((Long) query.uniqueResult()).intValue();

		// 获取结果集
		query = hibernateTemplate.getSessionFactory().getCurrentSession()
				.createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}

		int totalPage = total/pageSize;
		if(total % pageSize > 0) {
			totalPage += 1;
		}
		if(totalPage == 0) {
			totalPage = 1;
		}
		if(curPage>totalPage){
			curPage = totalPage;
		}
		query.setFirstResult((curPage - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<Object> datas = query.list();
		
		// 返回PagerModel
		PagerModel pm = new PagerModel();
		pm.setCurPage(curPage);
		pm.setDatas(datas);
		pm.setTotal(total);
		pm.setTotalPage(totalPage);
		
		return pm;
	}

	/**
	 * 根据HQL语句，获得查询总记录数的HQL语句
	 * 
	 * @param hql
	 * @return
	 */
	private String getCountQuery(String hql) {
		int index = hql.indexOf("from");
		if (index != -1) {
			return "select count(*) " + hql.substring(index);
		}
		throw new SystemException("无效的HQL查询语句【" + hql + "】");
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	/**
	 * 注入HibernateTemplate实体对象
	 * 
	 * @param hibernateTemplate
	 */
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@SuppressWarnings("rawtypes")
	public boolean exist(String hql, String para) {
		boolean exist = false;
		List lt = hibernateTemplate.find(hql, para);
		if (lt != null && lt.size() > 0) {
			exist = true;
		}
		return exist;
	}

	@SuppressWarnings("rawtypes")
	public boolean exist(String hql, Object obj1, Object obj2) {
		boolean exist = false;
		List lt = hibernateTemplate.find(hql, new Object[] { obj1, obj2 });
		if (lt != null && lt.size() > 0) {
			exist = true;
		}
		return exist;
	}

}
